package cn.hs.ph.servlet;

import cn.hs.ph.dao.CommodityDao;
import cn.hs.ph.dao.impl.CommodityDaoImpl;
import cn.hs.ph.entity.Commodity;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/commodity")
public class CommodityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CommodityDao commodityDao = new CommodityDaoImpl();
        String action = req.getParameter("action");
        if("selectAll".equals(action)){
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String goodsName = req.getParameter("goodsName");
            String goodsStatus = req.getParameter("goodsStatus");
            String goodsShopName = req.getParameter("shopName");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            Map map = new HashMap();
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            map.put("goodsName", goodsName);
            map.put("goodsStatus", goodsStatus);
            map.put("goodsShopName", goodsShopName);
            map.put("startTime", startTime);
            map.put("endTime", endTime);

            List <Commodity> commodityList = commodityDao.selectAll(map);
            int total = commodityDao.selectCount(map);
            Map resuleMap = new HashMap();
            resuleMap.put("list", commodityList);
            resuleMap.put("total", total);
            Result result = Result.success(resuleMap);
            resp.getWriter().write(JSON.toJSONString(result));
        }
        else if ("modifyShopGoods".equals(action)) {
            String Id = req.getParameter("goodsId");
            String Status = req.getParameter("goodsStatus");

            // 检查参数是否为 null
            if (Id == null || Status == null) {
                Result result = Result.error("参数缺失！");
                resp.getWriter().write(JSON.toJSONString(result));
                return; // 提前返回
            }

            try {
                int goodsId = Integer.parseInt(Id);
                int goodsStatus = Integer.parseInt(Status);
                Commodity commodity = new Commodity(goodsId, goodsStatus);
                int num = commodityDao.update(commodity);
                Result result = null;
                if (num != 0) {
                    result = Result.success("修改成功！");
                } else {
                    result = Result.error("修改失败！");
                }
                resp.getWriter().write(JSON.toJSONString(result));
            } catch (NumberFormatException e) {
                Result result = Result.error("参数格式错误！");
                resp.getWriter().write(JSON.toJSONString(result));
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
